<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象排序</title>
  </head>

  <body>

  </body>
  <script>
    /* 
    将数组里的对象按照年龄进行升序排序
    */
    let arr = [{
        name: '马冬梅',
        age: 89,
        gender: 'female'
      },
      {
        name: '周冬雨',
        age: 25,
        gender: 'female'
      },
      {
        name: '周杰伦',
        age: 136,
        gender: 'male'
      },
      {
        name: '桂伦镁',
        age: 39,
        gender: 'female'
      },
    ];
    var arr2 = [1, 2, 3]
    console.log(arr);
    arr.sort((a, b) => {
      if (a > b) {
        return a.age - b.age
      } else {
        return b.age - a.age
      }
    })
    console.log(arr);

    /* 
    数组排序升序排序
    */
    /* let numArr = [20, 30, 8, 15, 100];
    numArr.sort((a, b) => {
      if (a > b) {
        return 1
      } else if (a < b) {
        return -1
      } else {
        return 0
      }
    });
    console.log(numArr); */


    // 对象排序的方法
    // objSort(propertyName) { //对象排序
    //   return function (obj1, obj2) {
    //     let value1 = obj1[porpertyName];
    //     let value2 = obj2[porpertyName];
    //     if (value1 < value2) {
    //       return -1;
    //     } else if (value1 > value2) {
    //       return 1;
    //     } else {
    //       return 0;
    //     }
    //   }
    // },
    // upSort() {
    //   const {
    //     keyWord,
    //     arr
    //   } = this;
    //   let ageSort = arr.sort(this.objSort('age'))
    //   console.log(ageSort);
    // },



    // 冒泡排序

    /*  let temp = 0;
     for (let j = 0; j < arr.length - 1; j++) {
       for (let i = j; i < arr.length - 1; i++) {
         // console.log(arr[i].age);
         if (arr[i].age > arr[i + 1].age) { //升序排列
           temp = arr[i].age;
           arr[i].age = arr[i + 1].age;
           arr[i + 1].age = temp;
         }
       }
     }
     console.log(arr); */
  </script>

</html>